﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using SingleUserMall.Article.Form;
using SingleUserMall.Article.VO;
using SingleUserMall.Common;
using static SingleUserMall.Article.Articles;

namespace SingleUserMall.Article
{

    public class ArticlesAppService : SingleUserMallAppServiceBase, IArticlesAppService
    {
        private readonly IRepository<Articles, long> _articlesRepository;

        public ArticlesAppService(IRepository<Articles, long> articlesRepository)
        {
            _articlesRepository = articlesRepository;

        }

        public async Task<PagedResultDto<ArticlesCounselVO>> GetArticlesCounselListAsync(PagedForm form)
        {
            var query = _articlesRepository.GetAll().Where(x => x.ArticleType == ArticlesType.Counsel)
                .OrderByDescending(x => x.CreationTime);
            return await query.ToPagedResultDtoAsync<Articles, ArticlesCounselVO>(form, (i, vo) => { vo.CreateTime = i.CreationTime; });


        }
        public async Task<ArticlesAppDetailVO> GetArticlesCounselAsync(SingleId<long> form)
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.Id == form.Id);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ListResultDto<ArticlesAppVO>> GetArticlesHepleListAsync()
        {
            var query = _articlesRepository.GetAll().Where(x => x.ArticleType == ArticlesType.Helpe);
            return await query.ToListResultDtoAsync<Articles, ArticlesAppVO>((i, vo) => { });
        }
        public async Task<ArticlesAppDetailVO> GetArticlesHepleAsync(SingleId<long> form)
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.Id == form.Id);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }
        public async Task<ArticlesAppDetailVO> GetArticlesAboutAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.About);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesGroupsAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.Group);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesBargarinAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.Bargain);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesPrivacyPolicyAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.PrivacyPolicy);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesServiceAgreementAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.ServiceAgreement);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesUserAgreementAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.UserAgreement);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }
        public async Task<ArticlesAppDetailVO> GetArticlesUserFXAgreementAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.FXAgreement);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesUserFHAgreementAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.FHAgreement);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesUserQMAgreementAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.QMAgreement);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }

        public async Task<ArticlesAppDetailVO> GetArticlesActiveAgreementAsync()
        {
            var query = await _articlesRepository.FirstOrDefaultAsync(x => x.ArticleType == ArticlesType.ActiveAgreement);
            return query.ToVO<Articles, ArticlesAppDetailVO>((i, vo) => { vo.CreateTime = i.CreationTime; });
        }
    }
}
